package jaoso.framework.dao.hibernate;

import jaoso.framework.dao.BaseDAO;
import jaoso.framework.dao.MyQuery;
import jaoso.framework.dao.RightDAO;

import jaoso.framework.domain.Right;
import jaoso.framework.domain.Role;

import jaoso.framework.exception.DAOException;

import jaoso.framework.util.MyUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;

import java.util.Iterator;
import java.util.Set;

/**
 * @author Administrator
 * @version 0.9.1
 * @since 2004-5-29 Right DAO, persistent Right.
 */
public class RightDAOImpl implements RightDAO
{
    /** log */
    private static Log log = LogFactory.getLog(RightDAOImpl.class);

    /** dao */
    private BaseDAO baseDAO;

    /**
     * Creates a new RightDAOImpl object.
     */
    public RightDAOImpl()
    {
        super();
    }

    /**
     * @param dao
     *            dao
     */
    public final void setBaseDAO(final BaseDAO dao)
    {
        this.baseDAO = dao;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#isExist(java.lang.String)
     */
    public final boolean isExist(final String name)
    {
        boolean flage = false;

        try
        {
            //flage = getRightByDesc(name) != null;
            flage = ((Integer) baseDAO
                    .findEntity("select count(*) from Right a where a.desc='"
                            + name + "'")[0]).intValue() > 0;
        } catch (DAOException e)
        {
            log.error("Right isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#isExist(java.lang.String,
     *      java.io.Serializable)
     */
    public final boolean isExist(final String name, final Serializable id)
    {
        boolean flage = false;

        try
        {
            Right right = getRightByDesc(name);
            flage = (right != null) && !right.getId().equals(id);
        } catch (DAOException e)
        {
            log.error("Right isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#isExist(jaoso.framework.domain.Right)
     */
    public final boolean isExist(final Right right)
    {
        boolean flage = false;

        if (right == null)
        {
            return flage;
        }

        try
        {
            /*
             * Object[] objs = findRight("from Right a where a.box='" +
             * right.getBox() + "' or a.desc='" + right.getDesc() + "' or
             * a.url='" + right.getUrl() + "'"); flage = !MyUtils.isBlank(objs);
             *  
             */
            flage = ((Integer) baseDAO
                    .findEntity("select count(*) from Right a where a.box='"
                            + right.getBox() + "' or a.desc='"
                            + right.getDesc() + "' or a.url='" + right.getUrl()
                            + "'")[0]).intValue() > 0;
        } catch (DAOException e)
        {
            log.error("Right isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#isExist4u(jaoso.framework.domain.Right)
     */
    public final boolean isExist4u(final Right right)
    {
        boolean flage = false;

        if (right == null)
        {
            return flage;
        }

        try
        {
            /*
             * Right[] objs = findRight("from Right a where (a.box='" +
             * right.getBox() + "' or a.desc='" + right.getDesc() + "' or
             * a.url='" + right.getUrl() + "') and a.id <>'" + right.getId() +
             * "'");
             * 
             * if (!MyUtils.isBlank(objs)) {
             * 
             * flage = true; }
             */
            flage = ((Integer) baseDAO
                    .findEntity("select count(*) from Right a where (a.box='"
                            + right.getBox() + "' or a.desc='"
                            + right.getDesc() + "' or a.url='" + right.getUrl()
                            + "') and a.id<>'" + right.getId() + "'")[0])
                    .intValue() > 0;
        } catch (DAOException e)
        {
            log.error("Right isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#getRight(java.io.Serializable)
     */
    public final Right getRight(final Serializable id)
    {
        return (Right) baseDAO.loadEntity(Right.class, id);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#getRightByBox(java.lang.String)
     */
    public final Right getRightByBox(final String box)
    {
        if (MyUtils.isBlank(box))
        {
            return null;
        }

        Right[] rights = findRight("from Right a where a.box='" + box
                + "' order by a.box");

        if (MyUtils.isBlank(rights))
        {
            return null;
        }

        return rights[0];
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#getRightByName(java.lang.String)
     */
    public final Right getRightByDesc(final String name)
    {
        if (MyUtils.isBlank(name))
        {
            return null;
        }

        Right[] rights = findRight("from Right a where a.desc='" + name
                + "' order by a.box");

        if (MyUtils.isBlank(rights))
        {
            return null;
        }

        return rights[0];
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#createRight(jaoso.framework.domain.Right)
     */
    public final void createRight(final Right right)
    {
        baseDAO.saveEntity(right);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#findAllRight()
     */
    public final Right[] findAllRight()
    {
        return (Right[]) findRight("from Right a order by a.box");
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#findRight(java.lang.String)
     */
    public final Right[] findRight(final String query)
    {
        MyQuery myquery = new MyQuery();
        myquery.setQueryString(query);

        return findRight(myquery);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#findRight(jaoso.framework.dao.MyQuery)
     */
    public final Right[] findRight(final MyQuery query)
    {
        Object[] os = baseDAO.findEntity(query);

        if (MyUtils.isBlank(os))
        {
            return null;
        }

        Right[] entitys = new Right[os.length];
        System.arraycopy(os, 0, entitys, 0, os.length);

        return entitys;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#removeRight(jaoso.framework.domain.Right)
     */
    public final void removeRight(final Right right)
    {
        Set groups = right.getGroups();
        Role group;

        if ((groups != null) && (groups.size() > 0))
        {
            for (Iterator i = groups.iterator(); i.hasNext();)
            {
                group = (Role) i.next();
                group.getRights().remove(right);
                baseDAO.updateEntity(group);
            }

            right.setGroups(null);
        }

        baseDAO.removeEntity(right);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#removeRight(java.io.Serializable)
     */
    public final void removeRight(final Serializable id)
    {
        if (id == null)
        {
            throw new DAOException("id can't be null");
        }

        removeRight(getRight(id));
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#removeRight(java.io.Serializable[])
     */
    public final void removeRight(final Serializable[] ids)
    {
        if (ids == null)
        {
            throw new DAOException("id can't be null");
        }

        for (int i = 0, n = ids.length; i < n; i++)
        {
            removeRight(getRight(ids[i]));
        }
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.dao.RightDAO#updateRight(jaoso.framework.domain.Right)
     */
    public final void updateRight(final Right right)
    {
        baseDAO.updateEntity(right);
    }
}